import {nextTick,reactive} from "vue";
import {ElMessage} from "element-plus";
import {reviewUser} from "/@/api/api";
import {Local,Session} from "/@/utils/storage";
import { useRouter } from 'vue-router'

export const standingMounted = (refs:any = []) => {
    const router = useRouter()
    const state = reactive({
        nameShow:false,
        phoneShow:false,
        codeShow:false,
        districtShow:false,
        drawer:false,

        codeBtnTextShow:120,
        codeTime:undefined,

        name:'',
        phone:'',
        code:'',
        district:'',

        checkList:[],
    })
    const handleFocus1 = () => {
        state.nameShow = true
        nextTick(() => {
            refs[0].value.focus()
        })
    }
    const handleFocus2 = () => {
        state.drawer = true
    }
    const handleFocus3 = () => {
        state.phoneShow = true
        nextTick(() => {
            refs[1].value.focus()
        })
    }
    const handleFocus4 = () => {
        state.codeShow = true
        nextTick(() => {
            refs[2].value.focus()
        })
    }
    const blurNameFun = () => {
        if(state.name === ''){
            state.nameShow = false
        }
    }
    const blurPhoneFun = () => {
        if(state.phone === ''){
            state.phoneShow = false
        }
    }
    const codeBtnTextShowFun = () => {
        if(state.codeBtnTextShow === 120){
            state.codeTime = setInterval(()=>{
                state.codeBtnTextShow --
                if(state.codeBtnTextShow === 0){
                    clearInterval(state.codeTime)
                    state.codeBtnTextShow = 120
                }
            },1000)
        }
    }
    const blurCodeFun = () => {
        if(state.code === ''){
            state.codeShow = false
        }
    }
    const selectAreac = (item:any) => {
        state.district = item
        state.drawer = false
        state.districtShow = true
    }

    const reviewUserFun = (statusIndex:boolean) => {
        if(state.name === '') {
            ElMessage.warning('请输入姓名')
            return
        }
        if(state.district === '') {
            ElMessage.warning('请选择地区')
            return
        }
        if(state.phone === '') {
            ElMessage.warning('请输入手机号')
            return
        }
        if(state.code === '') {
            ElMessage.warning('请输入验证码')
            return
        }
        if(!state.checkList.length) {
            ElMessage.warning('请勾选协议')
            return
        }
        reviewUser({
            username:state.name,
            identity:Session.get('token') === '0' ? '20':Session.get('token') ? '30':'10',
            gender:statusIndex ? '女':'男',
            province:state.district,
            mobile:state.phone,
            code:state.code
        }).then((res:any) => {
            Local.set('sex',statusIndex ? '0':'1')
            Local.set('diqu',{
                district:state.district,
                name:state.name,
                phone:state.phone
            })

            if(res.data.code == 1){
                if(res.data.data.user != null){
                    Local.set('userInfo',res.data.data)
                    router.push({
                        path: '/copywriting'
                    })
                } else {
                    if(Object.keys(res.data.data.list).length != 0 && Session.get('token') === '2'){
                        Local.set('userInfo',res.data.data)
                        router.push({
                            path: '/copywriting'
                        })
                    } else {
                        ElMessage.error('非员工请选择其他')
                        router.push({
                            path: '/home'
                        })
                        Session.clear()
                    }

                }

            } else {
                ElMessage.error(res.data.msg)
            }
        })
    }

    return {
        stateMixin:state,
        handleFocus1,
        handleFocus2,
        handleFocus3,
        handleFocus4,
        blurNameFun,
        blurPhoneFun,
        codeBtnTextShowFun,
        blurCodeFun,
        selectAreac,
        reviewUserFun
    }
}